package me.zhujin.java.learnjava.myserver.webxml;

import me.zhujin.java.learnjava.myserver.webxml.MappingXml;
import me.zhujin.java.learnjava.myserver.webxml.ServletXml;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhujin on 10/24/16.
 * blog:www.zhujin.me
 * email: zhujin.nova@gmail.com
 */
public class WebHandler extends DefaultHandler {

    private List<ServletXml> servletXmlList;

    private List<MappingXml> mappingXmlList;

    private String beginTag;
    private boolean isMap;


    private ServletXml servletXml;
    private MappingXml mappingXml;
    private StringBuffer content;
    @Override
    public void startDocument() throws SAXException {
        //System.out.println("处理文档开始...................");
        servletXmlList = new ArrayList<ServletXml>();
        mappingXmlList = new ArrayList<MappingXml>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        //System.out.println("处理元素开始..................." + qName);
        content = new StringBuffer();
        if (null != qName) {
            beginTag = qName;

            if (qName.equals("servlet")) {
                isMap = false;
                servletXml = new ServletXml();
            } else if (qName.equals("servlet-mapping")) {
                isMap = true;
                mappingXml = new MappingXml();
            }
        }
    }


    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        content.append(new String(ch, start, length).trim());
        String str = content.toString();
        //System.out.println("逻辑处理...................beginTag:"+beginTag+"  str:" + str);
        if (null != beginTag) {
//            String str = new String(ch, start, length);
            if (isMap) {
                if (beginTag.equals("url-pattern")) {
                    if (null == mappingXml.getUrlPattern()) {
                        mappingXml.setUrlPattern(new ArrayList<String>());
                    }
                    mappingXml.getUrlPattern().add(str);
                } else if (beginTag.equals("servlet-name")) {
                    mappingXml.setServletName(str);
                }
            } else {
                if (beginTag.equals("servlet-name")) {
                    servletXml.setServletName(str);
                } else if (beginTag.equals("servlet-class")) {
                    servletXml.setClz(str);
                }
            }
        }
    }


    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        //System.out.println("元素处理结束..................." + qName);
        if (null != qName) {
            if (qName.equals("servlet")) {
                servletXmlList.add(servletXml);
            } else if (qName.equals("servlet-mapping")) {
                mappingXmlList.add(mappingXml);
            }
        }
    }


    @Override
    public void endDocument() throws SAXException {
        //System.out.println("文档处理结束...................");
    }

    public List<ServletXml> getServletXmlList() {
        return servletXmlList;
    }


    public List<MappingXml> getMappingXmlList() {
        return mappingXmlList;
    }


//    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//
//
//        SAXParserFactory factory = SAXParserFactory.newInstance();
//        SAXParser sax = factory.newSAXParser();
////        WebTestHandler webHandler = new WebTestHandler();
////        sax.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream(Const.WEB_XML), webHandler);
//
//
//        WebHandler webHandler = new WebHandler();
//        sax.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream(Const.WEB_XML), webHandler);
//        for (ServletXml servletXml: webHandler.getServletXmlList()) {
//            //System.out.println(servletXml);
//        }
//    }
}
